package com.grass.p019lv.activity;

import android.content.Intent;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.androidx.p013lv.base.listener.ItemClickListener;
import com.androidx.p013lv.base.p015ui.BaseActivity;
import com.androidx.p013lv.base.utils.NetUtil;
import com.androidx.p013lv.base.utils.SpUtils;
import com.androidx.p013lv.base.utils.UiUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.grass.p019lv.R;
import com.grass.p019lv.adapter.MineDownLoadVideoAdapter;
import com.grass.p019lv.bean.DownloadVideoBean;
import com.grass.p019lv.databinding.ActivityMineDownLoadVideoLayoutBinding;
import com.grass.p019lv.service.DownloadVideoService;
import com.gyf.immersionbar.ImmersionBar;
import com.luck.picture.lib.config.PictureConfig;
/*import com.p020lv.downloadvideo.M3U8Downloader;
import com.p020lv.downloadvideo.OnDeleteTaskListener;
import com.p020lv.downloadvideo.OnM3U8DownloadListener;
import com.p020lv.downloadvideo.bean.M3U8Task;
import com.p020lv.downloadvideo.utils.DataCacheUtils;
import com.p020lv.downloadvideo.utils.MUtils;*/
import com.yanzhenjie.recyclerview.OnItemMenuClickListener;
import com.yanzhenjie.recyclerview.SwipeMenu;
import com.yanzhenjie.recyclerview.SwipeMenuBridge;
import com.yanzhenjie.recyclerview.SwipeMenuCreator;
import com.yanzhenjie.recyclerview.SwipeMenuItem;
import com.yanzhenjie.recyclerview.SwipeRecyclerView;
import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import jaygoo.library.m3u8downloader.M3U8Downloader;
import jaygoo.library.m3u8downloader.OnDeleteTaskListener;
import jaygoo.library.m3u8downloader.OnM3U8DownloadListener;
import jaygoo.library.m3u8downloader.bean.M3U8Task;
import jaygoo.library.m3u8downloader.utils.MUtils;

/* renamed from: com.grass.lv.activity.MineDownLoadVideoActivity */
public class MineDownLoadVideoActivity extends BaseActivity<ActivityMineDownLoadVideoLayoutBinding> implements ItemClickListener {
    private MineDownLoadVideoAdapter adapter;
    private RecyclerView.AdapterDataObserver dataObserver = new RecyclerView.AdapterDataObserver() {
        /* class com.grass.p019lv.activity.MineDownLoadVideoActivity.C28791 */

        @Override // androidx.recyclerview.widget.RecyclerView.AdapterDataObserver
        public void onChanged() {
            MineDownLoadVideoActivity.this.deleteChangeState();
        }

        @Override // androidx.recyclerview.widget.RecyclerView.AdapterDataObserver
        public void onItemRangeChanged(int i, int i2) {
            MineDownLoadVideoActivity.this.deleteChangeState();
        }
    };
    private ArrayList<DownloadVideoBean> downLoadTask = new ArrayList<>();
    private boolean isEdit;
    private Long lastId = 0L;
    private Gson mGson = new Gson();
    OnItemMenuClickListener mItemMenuClickListener = new OnItemMenuClickListener() {
        /* class com.grass.p019lv.activity.MineDownLoadVideoActivity.C28824 */

        @Override // com.yanzhenjie.recyclerview.OnItemMenuClickListener
        public void onItemClick(SwipeMenuBridge swipeMenuBridge, int i) {
            swipeMenuBridge.closeMenu();
            MineDownLoadVideoActivity.this.deleteSingleCollect(i);
        }
    };
    SwipeMenuCreator mSwipeMenuCreator = new SwipeMenuCreator() {
        /* class com.grass.p019lv.activity.MineDownLoadVideoActivity.C28813 */

        @Override // com.yanzhenjie.recyclerview.SwipeMenuCreator
        public void onCreateMenu(SwipeMenu swipeMenu, SwipeMenu swipeMenu2, int i) {
            SwipeMenuItem swipeMenuItem = new SwipeMenuItem(MineDownLoadVideoActivity.this);
            swipeMenuItem.setBackgroundColorResource(R.color.mainColor);
            swipeMenuItem.setWidth(UiUtils.dp2px(70));
            swipeMenuItem.setHeight(-1);
            swipeMenuItem.setTextSize(16);
            swipeMenuItem.setText("刪除");
            swipeMenuItem.setTextColorResource(R.color.white);
            swipeMenu2.setOrientation(1);
            swipeMenu2.addMenuItem(swipeMenuItem);
        }
    };
    private OnM3U8DownloadListener onM3U8DownloadListener = new OnM3U8DownloadListener() {
        /* class com.grass.p019lv.activity.MineDownLoadVideoActivity.C28846 */

        @Override // com.p020lv.downloadvideo.OnM3U8DownloadListener
        public void onDownloadItem(M3U8Task m3U8Task, long j, int i, int i2) {
            super.onDownloadItem(m3U8Task, j, i, i2);
        }

        @Override // com.p020lv.downloadvideo.OnM3U8DownloadListener
        public void onDownloadSuccess(M3U8Task m3U8Task) {
            super.onDownloadSuccess(m3U8Task);
            MineDownLoadVideoActivity.this.adapter.notifyChanged(MineDownLoadVideoActivity.this.downLoadTask, m3U8Task);
        }

        @Override // com.p020lv.downloadvideo.OnM3U8DownloadListener
        public void onDownloadPending(M3U8Task m3U8Task) {
            super.onDownloadPending(m3U8Task);
            MineDownLoadVideoActivity.this.notifyChanged(m3U8Task);
        }

        @Override // com.p020lv.downloadvideo.OnM3U8DownloadListener
        public void onDownloadPause(M3U8Task m3U8Task) {
            super.onDownloadPause(m3U8Task);
            MineDownLoadVideoActivity.this.notifyChanged(m3U8Task);
        }

        @Override // com.p020lv.downloadvideo.OnM3U8DownloadListener
        public void onDownloadProgress(M3U8Task m3U8Task) {
            super.onDownloadProgress(m3U8Task);
            MineDownLoadVideoActivity.this.notifyChanged(m3U8Task);
        }

        @Override // com.p020lv.downloadvideo.OnM3U8DownloadListener
        public void onDownloadPrepare(M3U8Task m3U8Task) {
            super.onDownloadPrepare(m3U8Task);
            MineDownLoadVideoActivity.this.notifyChanged(m3U8Task);
        }

        @Override // com.p020lv.downloadvideo.OnM3U8DownloadListener
        public void onDownloadError(final M3U8Task m3U8Task, Throwable th) {
            super.onDownloadError(m3U8Task, th);
            if (NetUtil.isNetworkAvailable()) {
                new Handler().postDelayed(new Runnable() {
                    /* class com.grass.p019lv.activity.MineDownLoadVideoActivity.C28846.RunnableC28851 */

                    public void run() {
                        M3U8Downloader.getInstance().download(m3U8Task.getUrl());
                    }
                }, 5000);
            }
        }
    };

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void deleteSingleCollect(int i) {
    }

    @Override // com.androidx.p013lv.base.p015ui.BaseActivity
    public int setLayout() {
        return R.layout.activity_mine_down_load_video_layout;
    }

    /* access modifiers changed from: protected */
    @Override // com.androidx.p013lv.base.p015ui.BaseActivity
    public void initImmersionBar() {
        super.initImmersionBar();
        ImmersionBar.with(this).titleBar(((ActivityMineDownLoadVideoLayoutBinding) this.binding).toolbar).init();
    }

    /* access modifiers changed from: protected */
    @Override // com.androidx.p013lv.base.p015ui.BaseActivity
    public void initView() {
        super.initView();
        ((ActivityMineDownLoadVideoLayoutBinding) this.binding).setClick(new OnClick());
        ((ActivityMineDownLoadVideoLayoutBinding) this.binding).setIsEdit(Boolean.valueOf(this.isEdit));
        ((ActivityMineDownLoadVideoLayoutBinding) this.binding).setSelectNum(0);
        ((ActivityMineDownLoadVideoLayoutBinding) this.binding).recycler.setOnItemMenuClickListener(this.mItemMenuClickListener);
        MineDownLoadVideoAdapter mineDownLoadVideoAdapter = new MineDownLoadVideoAdapter();
        this.adapter = mineDownLoadVideoAdapter;
        mineDownLoadVideoAdapter.setItemListener(this);
        ((ActivityMineDownLoadVideoLayoutBinding) this.binding).recycler.setLayoutManager(new LinearLayoutManager(this));
        ((ActivityMineDownLoadVideoLayoutBinding) this.binding).recycler.setAdapter(this.adapter);
        this.adapter.registerAdapterDataObserver(this.dataObserver);
    }

    /* access modifiers changed from: protected */
    @Override // com.androidx.p013lv.base.p015ui.BaseActivity
    public void initData() {
        super.initData();
        ((ActivityMineDownLoadVideoLayoutBinding) this.binding).setStatus(0);
        String loadListCache = DataCacheUtils.loadListCache(this);
        if (!TextUtils.isEmpty(loadListCache)) {
            this.downLoadTask.addAll((List) this.mGson.fromJson(loadListCache, new TypeToken<List<DownloadVideoBean>>() {
                /* class com.grass.p019lv.activity.MineDownLoadVideoActivity.C28802 */
            }.getType()));
            ArrayList<DownloadVideoBean> arrayList = this.downLoadTask;
            if (arrayList == null || arrayList.size() <= 0) {
                ((ActivityMineDownLoadVideoLayoutBinding) this.binding).setStatus(4);
                return;
            }
            for (int i = 0; i < this.downLoadTask.size(); i++) {
                if (M3U8Downloader.getInstance().checkM3U8IsExist(this.downLoadTask.get(i).getM3U8Task().getUrl())) {
                    this.downLoadTask.get(i).getM3U8Task().setState(3);
                } else {
                    this.downLoadTask.get(i).getM3U8Task().setState(5);
                }
                if (M3U8Downloader.getInstance().isCurrentTask(this.downLoadTask.get(i).getM3U8Task().getUrl())) {
                    this.downLoadTask.get(i).getM3U8Task().setState(2);
                }
            }
            M3U8Downloader.getInstance().setOnM3U8DownloadListener2(this.onM3U8DownloadListener);
            this.adapter.setDomain(SpUtils.getInstance().getString("domain"));
            this.adapter.setData(this.downLoadTask);
            return;
        }
        ((ActivityMineDownLoadVideoLayoutBinding) this.binding).setStatus(4);
    }

    @Override // com.androidx.p013lv.base.listener.ItemClickListener
    public void onItemClick(View view, int i) {
        if (!this.isEdit) {
            String url = ((DownloadVideoBean) this.adapter.getDataInPosition(i)).getM3U8Task().getUrl();
            if (M3U8Downloader.getInstance().checkM3U8IsExist(url)) {
                String m3U8Path = M3U8Downloader.getInstance().getM3U8Path(url);
                Intent intent = new Intent(this, VideoPlayFullActivity.class);
                intent.putExtra(PictureConfig.EXTRA_VIDEO_PATH, m3U8Path);
                startActivity(intent);
            } else if (NetUtil.isNetworkAvailable()) {
                M3U8Downloader.getInstance().download(url);
                startService(new Intent(this, DownloadVideoService.class));
            }
        } else {
            DownloadVideoBean downloadVideoBean = (DownloadVideoBean) this.adapter.getDataInPosition(i);
            int i2 = 1;
            if (((DownloadVideoBean) this.adapter.getDataInPosition(i)).getEditState() == 1) {
                i2 = 2;
            }
            downloadVideoBean.setEditState(i2);
            this.adapter.notifyItemChanged(i, Integer.valueOf((int) R.id.img_select));
        }
    }

    public static void isSwipeMenuList(SwipeRecyclerView swipeRecyclerView, boolean z) {
        swipeRecyclerView.setSwipeItemMenuEnabled(!z);
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void deleteChangeState() {
        if (this.isEdit) {
            int i = 0;
            for (int i2 = 0; i2 < this.adapter.getData().size(); i2++) {
                if (((DownloadVideoBean) this.adapter.getData().get(i2)).getEditState() == 2) {
                    i++;
                }
            }
            ((ActivityMineDownLoadVideoLayoutBinding) this.binding).setSelectNum(Integer.valueOf(i));
        }
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void deleteCollectVideo() {
        ArrayList arrayList = new ArrayList();
        Iterator it = this.adapter.getData().iterator();
        while (it.hasNext()) {
            DownloadVideoBean downloadVideoBean = (DownloadVideoBean) it.next();
            if (downloadVideoBean.getEditState() == 2) {
                arrayList.add(downloadVideoBean);
                it.remove();
            }
        }
        for (int i = 0; i < this.adapter.getData().size(); i++) {
            ((DownloadVideoBean) this.adapter.getData().get(i)).setEditState(0);
        }
        this.adapter.notifyDataSetChanged();
        for (int i2 = 0; i2 < arrayList.size(); i2++) {
            MUtils.clearDir(new File(MUtils.getSaveFileDir(((DownloadVideoBean) arrayList.get(i2)).getM3U8Task().getUrl())));
            M3U8Downloader.getInstance().cancelAndDelete(((DownloadVideoBean) arrayList.get(i2)).getM3U8Task().getUrl(), new OnDeleteTaskListener() {
                /* class com.grass.p019lv.activity.MineDownLoadVideoActivity.C28835 */

                @Override // com.p020lv.downloadvideo.BaseListener
                public void onError(Throwable th) {
                }

                @Override // com.p020lv.downloadvideo.OnDeleteTaskListener
                public void onFail() {
                }

                @Override // com.p020lv.downloadvideo.OnDeleteTaskListener, com.p020lv.downloadvideo.BaseListener
                public void onStart() {
                }

                @Override // com.p020lv.downloadvideo.OnDeleteTaskListener
                public void onSuccess() {
                }
            });
        }
        this.downLoadTask.removeAll(arrayList);
        DataCacheUtils.saveListCache(this, this.mGson.toJson(this.downLoadTask));
    }

    /* access modifiers changed from: protected */
    @Override // androidx.appcompat.app.AppCompatActivity, androidx.fragment.app.FragmentActivity, com.androidx.p013lv.base.p015ui.BaseActivity
    public void onDestroy() {
        this.adapter.unregisterAdapterDataObserver(this.dataObserver);
        super.onDestroy();
    }

    /* renamed from: com.grass.lv.activity.MineDownLoadVideoActivity$OnClick */
    public class OnClick {
        public OnClick() {
        }

        public void btnBack() {
            MineDownLoadVideoActivity.this.finish();
        }

        public void editCollect() {
            MineDownLoadVideoActivity mineDownLoadVideoActivity = MineDownLoadVideoActivity.this;
            mineDownLoadVideoActivity.isEdit = !mineDownLoadVideoActivity.isEdit;
            ((ActivityMineDownLoadVideoLayoutBinding) MineDownLoadVideoActivity.this.binding).setIsEdit(Boolean.valueOf(MineDownLoadVideoActivity.this.isEdit));
            if (MineDownLoadVideoActivity.this.isEdit) {
                for (int i = 0; i < MineDownLoadVideoActivity.this.adapter.getData().size(); i++) {
                    ((DownloadVideoBean) MineDownLoadVideoActivity.this.adapter.getData().get(i)).setEditState(1);
                }
                MineDownLoadVideoActivity.this.adapter.notifyDataSetChanged();
                return;
            }
            for (int i2 = 0; i2 < MineDownLoadVideoActivity.this.adapter.getData().size(); i2++) {
                ((DownloadVideoBean) MineDownLoadVideoActivity.this.adapter.getData().get(i2)).setEditState(0);
            }
            MineDownLoadVideoActivity.this.adapter.notifyDataSetChanged();
        }

        public void allSelectedBtn() {
            for (int i = 0; i < MineDownLoadVideoActivity.this.adapter.getData().size(); i++) {
                ((DownloadVideoBean) MineDownLoadVideoActivity.this.adapter.getData().get(i)).setEditState(2);
            }
            MineDownLoadVideoActivity.this.adapter.notifyDataSetChanged();
        }

        public void deleteBtn() {
            MineDownLoadVideoActivity.this.deleteCollectVideo();
            MineDownLoadVideoActivity mineDownLoadVideoActivity = MineDownLoadVideoActivity.this;
            mineDownLoadVideoActivity.isEdit = !mineDownLoadVideoActivity.isEdit;
            ((ActivityMineDownLoadVideoLayoutBinding) MineDownLoadVideoActivity.this.binding).setIsEdit(Boolean.valueOf(MineDownLoadVideoActivity.this.isEdit));
        }
    }

    /* access modifiers changed from: protected */
    @Override // androidx.appcompat.app.AppCompatActivity, androidx.fragment.app.FragmentActivity
    public void onStop() {
        super.onStop();
    }

    /* access modifiers changed from: protected */
    @Override // androidx.fragment.app.FragmentActivity
    public void onPause() {
        super.onPause();
        ArrayList<DownloadVideoBean> arrayList = this.downLoadTask;
        if (arrayList != null && arrayList.size() > 1) {
            DataCacheUtils.saveListCache(this, this.mGson.toJson(this.downLoadTask));
        }
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void notifyChanged(final M3U8Task m3U8Task) {
        runOnUiThread(new Runnable() {
            /* class com.grass.p019lv.activity.MineDownLoadVideoActivity.RunnableC28867 */

            public void run() {
                MineDownLoadVideoActivity.this.adapter.notifyChanged(MineDownLoadVideoActivity.this.downLoadTask, m3U8Task);
            }
        });
    }
}
